<?php

require_once 'StudentMapper.php';
require_once 'Dbutils.php';
require_once 'Result.php';

/**
 * 学生服务类
 */
class StudentService
{

    private $studentMapper;

    public function __construct()
    {
        $this->studentMapper = new StudentMapper();
    }

    /**
     * 分页查询学生信息列表
     * @param Student $condition
     * @param Page $page
     * @return array 学生信息列表
     */
    public function listStudentInfoByPage(Student $condition, Page $page): Result
    {
        $total = $this->conditionCount($condition);
        if ($total > 0) {
            $connection = Dbutils::getConn();
            $result = mysqli_query($connection, $this->studentMapper->listStudentInfoByPage($condition, $page));
            if (!$result) {
                die("查询错误: " . $connection->error);
            }

            $students = [];
            while ($row = $result->fetch_assoc()) {
                $students[] = new Student(
                    $row['id'],
                    $row['name'],
                    $row['sex'],
                    $row['age'],
                    $row['city']
                );
            }
            $connection->close();
            return Result::page($students, $total, $page->cur);
        }
        return Result::empty();
    }

    /**
     * 通过学生编号检索学生信息
     * @param int $id 学生编号
     * @return Student|null
     */
    public function getById(int $id): Student|null
    {
        $connection = Dbutils::getConn();
        $result = $connection->query($this->studentMapper->getById($id));
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            return new Student($row["id"], $row['name'], $row['sex'], $row['age'], $row['city']);
        } else {
            return null;
        }
    }

    /**
     * 通过学生编号删除学生信息
     * @param int $id 学生编号
     * @return void
     */
    public function delById(int $id)
    {
        $connection = Dbutils::getConn();
        mysqli_query($connection, $this->studentMapper->deleteById($id));
    }

    /**
     * 插入学生信息
     * @param Student $student 待保存学生信息
     * @return void
     */
    public function insert(Student $student)
    {
        $connection = Dbutils::getConn();
        Assert::isTrue($connection->query($this->studentMapper->addStudent($student)), "学生信息保存失败，请检查");
    }

    public function fullCount(): int
    {
        $connection = Dbutils::getConn();
        $n = mysqli_query($connection, $this->studentMapper->fullCount());
        $row = mysqli_fetch_assoc($n);
        return intval($row['total_count']);
    }

    public function conditionCount(Student $condition): int
    {
        $connection = Dbutils::getConn();
        $n = mysqli_query($connection, $this->studentMapper->conditionCount($condition));
        $row = mysqli_fetch_assoc($n);
        $connection->close();
        return intval($row['total_count']);
    }

    public function getByUsername(string $username): User|null
    {
        $connection = Dbutils::getConn();
        $result = $connection->query($this->studentMapper->getByUsername($username));
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            return new User($row["userid"], $row['username'], $row['password'], $row['email']);
        } else {
            return null;
        }
    }

    public function getByEmail(string $email): User|null
    {
        $connection = Dbutils::getConn();
        $result = $connection->query($this->studentMapper->getByEmail($email));
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            return User::baseInfo($row["userid"], $row['username'], $row['email']);
        } else {
            return null;
        }
    }

    public function insertUser(User $saveInfo): void
    {
        $connection = Dbutils::getConn();
        Assert::isTrue($connection->query($this->studentMapper->insertUser($saveInfo)), "用户信息注册失败，请检查");
    }

}